package com.social.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/*
* 文件上传控制层
* */
@Controller
public class FileUploadController {
    @Value("${fileServicePath}")
    private String fileServicePath;
    /**
     * 实现文件上传
     * */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        Date dt = new Date();
        Long time = dt.getTime();

        // 文件原名称
        String originFileName = "";
        // 上传文件重命名
        String originalFilename = time.toString().substring(time.toString().length() - 12, time.toString().length());
        originalFilename = originalFilename.concat(".");
        originalFilename = originalFilename.concat(fileName.substring(file.getOriginalFilename().indexOf(".") + 1));

        String path =fileServicePath+ originalFilename;
        System.out.println(fileServicePath + "-->" + fileServicePath);
        File dest = new File(path);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            //返回绝对目录
            return "/imageService/"+originalFilename;
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }

    }
    /**
     * 实现多文件上传
     * */

    /**
     * public @ResponseBody String multifileUpload(@RequestParam("fileName")List<MultipartFile> files) */
    @RequestMapping(value="/multifileUpload",method=RequestMethod.POST)
    public @ResponseBody String multifileUpload(HttpServletRequest request){

        MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
//        List<MultipartFile> files = ((MultipartHttpServletRequest) request)
//                .getFiles("file");


        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");

        if(files.isEmpty()){
            return "false";
        }

        String path = "E:/test" ;

        for(MultipartFile file:files){
            String fileName = file.getOriginalFilename();
            int size = (int) file.getSize();
            System.out.println(fileName + "-->" + size);

            if(file.isEmpty()){
                return "false";
            }else{
                File dest = new File(path + "/" + fileName);
                if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
                    dest.getParentFile().mkdir();
                }
                try {
                    file.transferTo(dest);
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return "false";
                }
            }
        }
        return "true";

    }
}
